<template>
    <div class="internalTransactionRecordsPage">
        <el-radio-group v-model="tabPosition" style="margin-bottom: 30px" @change="change">
            <el-radio-button label="1">实时余额</el-radio-button>
            <el-radio-button label="0">历史余额</el-radio-button>
        </el-radio-group>
        <div class="search-wrap d-flex a-center" v-if="tabPosition==='1'">
            <div class="search-item d-flex a-center m-l20">
                <label>单位名称</label>
                <el-input class="m-l10" v-model="form.companyName" placeholder="请输入单位名称"></el-input>
            </div>
            <div class="search-item d-flex a-center m-l20">
                <label>内部户账号</label>
                <el-input class="m-l10"  v-model="form.innerAccountId" placeholder="请输入内部户账号"></el-input>
            </div>
            <div class="search-buttons m-l50 d-flex a-center">
                <el-button
                        color="#fc702b"
                        style="color:#fff;"
                        @click.prevent="onQuery"
                >查询</el-button>
                <el-button
                        color="#f0f0f0"
                        style="color: #000000"
                        @click.prevent="onReset"
                >重置</el-button>
                <el-divider direction="vertical" />
                <el-button
                    class="button_edit m-r10"
                    @click="toDetail"
                >
                    查看明细
                </el-button>
                <el-button
                        class="button_edit m-r10"
                        @click="onExportType('1')"
                >
                    <svg-icon icon-class="project-detail-icon-derive" class="icon-export m-r5"></svg-icon>
                    一键导出
                </el-button>
                <el-button
                        class="button_edit"
                        @click="onImport('1')"
                >
                    <svg-icon icon-class="project-detail-icon-l-xiazai" class="icon-export m-r5"></svg-icon>
                    导入
                </el-button>
            </div>
        </div>
        <div class="search-wrap d-flex a-center flex-wrap" v-if="tabPosition==='0'">
            <div class="search-item d-flex a-center m-l20 m-b10">
                <label>单位名称</label>
                <el-input class="m-l10" v-model="form.companyName" placeholder="请输入单位名称"></el-input>
            </div>
            <div class="search-item d-flex a-center m-l20 m-b10">
                <label>内部户账号</label>
                <el-input class="m-l10"  v-model="form.innerAccountId" placeholder="请输入内部户账号"></el-input>
            </div>
            <div class="search-item d-flex a-center m-l20 m-b10">
                <label>交易日期</label>
                <el-date-picker
                    class="m-l10"
                    v-model="form.dateTime"
                    value-format="YYYY-MM-DD"
                    type="daterange"
                    size="large"
                    range-separator="-"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
                    @change="datePickerChange"
                />
            </div>
            <div class="search-buttons m-l50 d-flex a-center m-b10">
                <el-button
                        color="#fc702b"
                        style="color:#fff;"
                        @click.prevent="onQuery"
                >查询</el-button>
                <el-button
                        color="#f0f0f0"
                        style="color: #000000"
                        @click.prevent="onReset"
                >重置</el-button>
                <el-divider direction="vertical" />
                <el-button
                        class="button_edit m-r10"
                        @click="onExportType('0')"
                >
                    <svg-icon icon-class="project-detail-icon-derive" class="icon-export m-r5"></svg-icon>
                    一键导出
                </el-button>
                <el-button
                        class="button_edit"
                        @click="onImport('0')"
                >
                    <svg-icon icon-class="project-detail-icon-l-xiazai" class="icon-export m-r5"></svg-icon>
                    导入
                </el-button>
            </div>
        </div>
        <public-table
                ref="publicTableRef"
                class="m-t20"
                index
                radio
                :height="550"
                :indexWidth="'100'"
                :page="page"
                :tableData="tableData"
                :tableOption.sync="tabPosition==='1'?tableOption:tableOption2"
                @page-change="onPageChange"
                @handleSelectionChange="handleSelectionChange"
        >
        </public-table>
    </div>

    <import-file-popup
            :title="title"
            :is-tip="true"
            ref="importFilePopupRef"
            :interface-config="importFileInterfaceConfig"
            :template-config="templateConfig"
            @on-success="onQuery"
    />
</template>

<script setup>
import {onMounted, reactive, ref} from "vue";
import {
    ElSelect,
    ElOption,
    ElDivider,
    ElMessage,
    ElDatePicker, ElRadioGroup, ElRadioButton
} from "element-plus";
import PublicTable from "@/components/PublicTable.vue";

import {tableCommonFun} from "@/hooks/tableCommon";
import {bankManagementApi, companyApi} from "@/api/businessFinance";
import {useRouter} from "vue-router";
import ImportFilePopup from "@/components/importFilePopup.vue";
import router from "@/router";

const title = ref('导入实时余额表');
const interfaceConfig = reactive({
    fileName:'businessFinance',
    apiPath:'CBS.getInnerBalanceList',
});
const importFileInterfaceConfig = reactive({
    fileName:'businessFinance',
    apiPath:'CBS.importInnerBalance',
});
const exportConfig = reactive({
    fileName:'businessFinance',
    apiPath:'CBS.getBankInnerBalanceLeadingOut',
    tableName:'CBS内部户余额导出'
});
const templateConfig = reactive({
    fileName:'businessFinance',
    apiPath:'CBS.downloadMB',
    param:{type:3},
    templateName:'CBS内部户实时余额模版'
});


const {
    form,
    page,
    tableOption,
    tableData,
    selectedArr,
    onQuery,
    onReset,
    onPageChange,
    getData,
    handleSelectionChange,
    onExport
}  = tableCommonFun(interfaceConfig,exportConfig);

form.value = {
    companyName:'',
    innerAccountId:'',
    dateTime:'',
    startTime:'',
    endTime:''
}

tableOption.value = [
    { label: "单位名称 ", prop: "companyName", minWidth: 100 },
    { label: "内部户账号 ", prop: "innerAccountId", minWidth: 100 },
    { label: "内部户名称 ", prop: "innerName", minWidth: 100 },
    { label: "存款类型 ", prop: "depositType", minWidth: 100 },
    { label: "账户余额 ", prop: "accountBalance", minWidth: 100 },
    { label: "冻结金额 ", prop: "freezeBalance", minWidth: 100 },
    { label: "可用余额 ", prop: "availableBalance", minWidth: 100 },
    { label: "透支额度 ", prop: "overdraftLimit", minWidth: 100 },
    { label: "占用透支 ", prop: "overdraft", minWidth: 100 },
    { label: "可用透支 ", prop: "availableOverdraft", minWidth: 100 },
    { label: "更新时间 ", prop: "cbsTime", minWidth: 100 },
];

const tableOption2 = ref([]);

tableOption2.value = [
    { label: "记账日期 ", prop: "recordTime", minWidth: 100 },
    { label: "单位名称 ", prop: "companyName", minWidth: 100 },
    { label: "内部户账号 ", prop: "innerAccountId", minWidth: 100 },
    { label: "内部户名称 ", prop: "innerName", minWidth: 100 },
    { label: "存款类型 ", prop: "depositType", minWidth: 100 },
    { label: "当日账户余额 ", prop: "dayBalance", minWidth: 100 },
    { label: "借方发生额 ", prop: "debtorBalance", minWidth: 100 },
    { label: "贷方发生额 ", prop: "creditBalance", minWidth: 100 },
    { label: "更新时间 ", prop: "cbsTime", minWidth: 100 },
];

const tabPosition  = ref('1');
const companyList = ref([]);
/**
 * 查公司列表
 */
const getCompanyList = () => {
    companyApi.getList({}).then(res=>{
        companyList.value = res.data;
    });
}

const visible = ref(false);
const interestForm = reactive({
    startTime:"",
    endTime:"",
    valueTime:''
});

/**
 * 新增计息计算
 *
 */
const addHandle = () => {
    if(selectedArr.value.length===0){
        ElMessage.warning('请选择一条要操作的数据!');
        return;
    }
    visible.value = true;
}

/**
 * 新增计息计算
 *
 */
const addInterest = (param,callback,err_callback) => {
    bankManagementApi.addInterest(param).then(res=>{
        if(res.code === '200'){
            if(callback)callback();
        }else{
            if(err_callback)err_callback();
        }
    });
}

/**
 * 新增计息计算确认
 *
 */
const onConfirm = () => {
    if(amountMoney.value===0){
        ElMessage.warning('计算计提计算结果不能为零,请点击计算按钮!');
        return;
    }
    formRef.value.validate((valid, fields) => {
        if (valid) {
            addInterest({
                loanRecordId:selectedArr.value[0].loanRecordId,
                startTime:interestForm.startTime,
                endTime:interestForm.endTime,
                calculateAmount:selectedArr.value[0].loanAmount,
                interestAmount:amountMoney.value,
            },(res)=>{
                setTimeout(()=>{
                    ElMessage.success('操作成功!');
                },1500);
                visible.value = false;
                publicTableRef.value.clearSelection();
                selectedArr.value = [];
                page.current = 1;
                page.size = 10;
                getData({
                    pageNum:1,
                    pageSize:10
                });
            },()=>{
                ElMessage.error('操作失败,请联系管理员!');
                visible.value = false;
                publicTableRef.value.clearSelection();
                selectedArr.value = [];
                page.current = 1;
                page.size = 10;
                getData({
                    pageNum:1,
                    pageSize:10
                });
            });
        } else {
            console.log('error submit!', fields)
        }
    });
}

/**
 * 计提时间被选择时触发
 *
 */
const onChange = (e) => {
    if(e===null || (e instanceof Array && e.length===0)){
        form.value.startTime = '';
        form.value.endTime = '';
        return;
    }
    form.value.startTime = e[0];
    form.value.endTime = e[1];
}

/**
 * 交易日期
 */
const datePickerChange = (e) => {
    form.value.startTime = '';
    form.value.endTime = '';
    if(e){
        form.value.startTime = e[0];
        form.value.endTime = e[1];
    }
}

/**
 *
 * @param e
 */
const change = (e) => {
  if(e==='1'){
    interfaceConfig.apiPath = 'CBS.getInnerBalanceList';
  }
  if(e==='0'){
      interfaceConfig.apiPath = 'CBS.getInnerHisBalanceList';
  }
  page.current = 1;
  page.size = 50;
  getData({
      pageNum:1,
      pageSize:50
  });
}

/**
 * 导出
 */
const onExportType = (type) => {
    if(type==='1'){
        exportConfig.apiPath = 'CBS.getBankInnerBalanceLeadingOut';
        exportConfig.tableName = 'CBS内部户实时余额导出';
    }
    if(type==='0'){
        exportConfig.apiPath = 'CBS.getBankInnerHisBalanceLeadingOut';
        exportConfig.tableName = 'CBS内部户历史余额导出';
    }
    onExport();
}

const importFilePopupRef = ref(null);

/**
 * 导入
 * @param type
 */
const onImport = (type) => {
    if(type==='1'){
        title.value = '导入cbs内部户实时余额';
        importFileInterfaceConfig.apiPath = 'CBS.importInnerBalance';
        templateConfig.param.type = 3;
        templateConfig.templateName = '银行信息实时余额表模版';
    }
    if(type==='0'){
        title.value = '导入cbs内部户历史余额';
        importFileInterfaceConfig.apiPath = 'CBS.importInnerHisBalance';
        templateConfig.param.type = 4;
        templateConfig.templateName = 'cbs内部户历史余额模版';
    }
    importFilePopupRef.value.openPopup();
}

const toDetail = () => {
  router.push({path:`/businessFinance/bankManagement/bankInformation/internalAccountTransactionRecords`});
}

onMounted(()=>{
    getData({
        pageNum:1,
        pageSize:50
    });
});
</script>

<style lang="scss" scoped>
.internalTransactionRecordsPage{
  .search-wrap{
    .search-item{

      .el-input{
        width: auto !important;
      }
    }
    .search-buttons{
      .icon-export{
        width: 18px;
        height: 18px;
      }
    }
  }
}

.public-dialog-content{
  .dialog-title-box{
    height: 30px;
    span{
      font-size: 15px;
      font-weight: 600;
    }
  }
}
</style>
